/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RunableClass;


import RunableClass.useClass.Addport;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import mysql.connect;
import net.ipaddress.authenticator;

/**
 *
 * @author Anatomy
 */
public class FavoriteRunnable implements Runnable{
    private JTable JT;
  //  private int i;

    public FavoriteRunnable(JTable JT) {
        this.JT = JT;
    }
    
   int count=1;int count1=0;
      static  StringBuffer     content        =   new StringBuffer(""); 
    @Override
    public void run() {
        for(;;){
   //////////////////////////////////////////////////////////////////////////
              //////////////////////////////////สร้างการดึงค่าจาก URL/////////////////////////////////////////////////////////////////////////////**/
     /**/         try {
         
              int count2=1;
         String buffer;
        String url = "http://www.settrade.com/C13_MarketSummaryStockType.jsp?type=S",                                                         /**/                                                      
     /**/          proxy = "proxy.up.ac.th",                                                                                        /**/
     /**/          port = "8080",                                                                                                   /**/
     /**/          username = "up\\51286885",                                                                                       /**/
     /**/          password = "7489250721";                                                                                         /**/
     /**/                                                                                                                           /**/
     /**/       //URL autoIP = new URL("http://checkip.dyndns.org:8245/");                                                          /**/
     /**/       //System.out.println("Get URL");                                                                                    /**/
     /**/                                                                                                                           /**/
     /**/     URL server = new URL(url);                                                                                            /**/
     /**/                                                                                                                           /**/
     /**/       Authenticator.setDefault(new authenticator(                                                                         /**/
     /**/                   username,password));                                                                                    /**/
     /**/       Properties systemProperties = System.getProperties();                                                               /**/
     /**/       systemProperties.setProperty("http.proxyHost",proxy);                                                               /**/
     /**/       systemProperties.setProperty("http.proxyPort",port);                                                                /**/
     /**/       HttpURLConnection connection = (HttpURLConnection)server.openConnection();                                          /**/
     /**/      connection.connect();                                                                                                /**/
     /**/                                                                                                                           /**/
     /**/                                                                                                                           /**/
     /**/       try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"TIS-620"))) {        /**/
     /**/                                                                                                                           /**/
     /**/                                                                                                                                /**/                                                                     /**/
     /**/                content = new StringBuffer("");                                                                                                             /**/
     /**/           while((buffer = in.readLine()) != null) {                                                                     /**/  
     /**/                  count2++;                                                                                              /**/  
     /**/                                                                                                                           /**/  
     /**/                                                                                                                         /**/  
     /**/                  content.append(count2).append("-->").append(buffer).append("\n");                                      /**/
     /**/                                                                                                                         /**/      
      /**/            }                                                                                                           /**/  
      /**/      }                                                                                                                 /**/  
     /**/             } catch (IOException io){
            System.err.println("Couldn't create help URL: " + io);
        

    }   
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            int countindex=0;

              try {
          String sqlsec="SELECT count(*) FROM `stock_favorite`";
                connect a=   new connect();
                  a.rs= a.stmt.executeQuery(sqlsec);
               a.rs.next();
                      
               countindex= Integer.parseInt(a.rs.getString(1));
             //   BrowserControl.displayURL(a.rs.getString("Link"));
           a.CloseDB();
          
       } catch (SQLException ex) {
       } 
              DefaultTableModel model=new DefaultTableModel(Col1,0);
              if(countindex>0){       
                  
          String Symbol[]=new String[countindex];  
              try {
          String sqlsec="SELECT * FROM `stock_favorite` order by (SymbolName) ASC";
                connect a=   new connect();
                  a.rs= a.stmt.executeQuery(sqlsec);
                  int indexarray=0;
               while(a.rs.next()){
                 Symbol[indexarray]=a.rs.getString("SymbolName");  
                 ++indexarray;
               }
           a.CloseDB();
       } catch (SQLException ex) {
       }
              
              
              for(int i=0;i<Symbol.length;i++){
               
        try {
              String[] Array= this.readFromFile(content.toString(),Symbol[i]);
                                
                //   String [] textArray ={Array[5],Array[6]};
                    if(Array[6].startsWith("-")){
                        
                             String xc[] ={Addport.Setstatic(Array[0],0).toString(),Array[1],Array[2],Array[3],Addport.Setstatic(Array[4],0).toString(),Addport.Setstatic(Array[5],0).toString() ,
                                 Addport.Setcolor(Array[6]).toString(),Addport.Setcolor(Array[7]).toString(),
                                 Array[8],Array[9],Array[10],Array[11]};
                    model.addRow(xc); 
                          }else if(Array[6].startsWith("+")){
                    String xc[] ={Addport.Setstatic(Array[0],1).toString(),Array[1],Array[2],Array[3],Addport.Setstatic(Array[4],0).toString(),Addport.Setstatic(Array[5],1).toString() ,
                                 Addport.Setcolor(Array[6]).toString(),Addport.Setcolor(Array[7]).toString(),
                                 Array[8],Array[9],Array[10],Array[11]}; 
                    model.addRow(xc); 
                          }else{
                    String xc[] ={Array[0],Array[1],Array[2],Array[3],Array[4],Addport.Setstatic(Array[5],2).toString(),
                                 Addport.Setcolor(Array[6]).toString(),Addport.Setcolor(Array[7]).toString(),
                                 Array[8],Array[9],Array[10],Array[11]};
                    model.addRow(xc); 
                          }
                   //String[] Col1 = {"SymbolName","Group","Open","Close","High","Low","Mk.Vol","Mk.Val('000)"};
   
                   // if(Array[6].startsWith("-")){
                    //   String xc[] ={Array[0],"",Array[2],Array[3],Array[4], Array[5],Array[10],Array[11]};
                  // model.addRow(xc); 
                     //     }else if(Array[6].startsWith("+")){
                      ///       String xc[]={Array[0],"",Array[2],Array[3],Array[4], Addport.Setcolor(Array[6]).toString(),Addport.Setcolor(Array[7]).toString(),Addport.Setcolor(Array[6]).toString(),"-",Addport.SetcolorArray(textArray).toString(),"-"};
                   //model.addRow(xc); 
                        //  }else{
                //        //      String xc[]={Array[0],"",Array[2],Array[3],Array[4], Addport.Setcolor(Array[6]).toString(),Addport.Setcolor(Array[7]).toString(),"-","-"};
                  // model.addRow(xc); 
                      //    }
                   
        }               catch (FileNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        } catch (SQLException ex) {
        }
              }
              
              
              
               }else{
                 String[] xc = {"","","","","","","",""};
               model.addRow(xc); 
              }
              
          
            

          JT.setModel(model);
         JT.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
       
         JT.getColumnModel().getColumn(0).setResizable(false);
         JT.getColumnModel().getColumn(0).setPreferredWidth(100);
         JT.getColumnModel().getColumn(1).setResizable(false);
         JT.getColumnModel().getColumn(1).setPreferredWidth(120);
         JT.getColumnModel().getColumn(2).setResizable(false);
         JT.getColumnModel().getColumn(2).setPreferredWidth(120);
         JT.getColumnModel().getColumn(3).setResizable(false);
         JT.getColumnModel().getColumn(3).setPreferredWidth(120);
         JT.getColumnModel().getColumn(4).setResizable(false);
         JT.getColumnModel().getColumn(4).setPreferredWidth(120);
         JT.getColumnModel().getColumn(5).setResizable(false);
         JT.getColumnModel().getColumn(5).setPreferredWidth(120);
         JT.getColumnModel().getColumn(6).setResizable(false);
         JT.getColumnModel().getColumn(6).setPreferredWidth(50);   
          JT.getColumnModel().getColumn(7).setResizable(false);
         JT.getColumnModel().getColumn(7).setPreferredWidth(50);
          JT.getColumnModel().getColumn(8).setResizable(false);
         JT.getColumnModel().getColumn(8).setPreferredWidth(120);
           JT.getColumnModel().getColumn(9).setResizable(false);
         JT.getColumnModel().getColumn(9).setPreferredWidth(120);
              JT.getColumnModel().getColumn(10).setResizable(false);
         JT.getColumnModel().getColumn(10).setPreferredWidth(120);
                  JT.getColumnModel().getColumn(11).setResizable(true);
         JT.getColumnModel().getColumn(11).setPreferredWidth(120);
                  DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
      String[] Col1 = {"SymbolName","Mark","Open","High","Low","Value","Chg","%Chg","Bids","Offer","Mk.Vol","Mk.Val('000)"};
    
dtcr.setHorizontalAlignment(SwingConstants.CENTER);
JT.getColumn("SymbolName").setCellRenderer(dtcr);
JT.getColumn("Value").setCellRenderer(dtcr);
JT.getColumn("Open").setCellRenderer(dtcr);
JT.getColumn("High").setCellRenderer(dtcr);
JT.getColumn("Low").setCellRenderer(dtcr);
JT.getColumn("Chg").setCellRenderer(dtcr);
JT.getColumn("%Chg").setCellRenderer(dtcr);
JT.getColumn("Bids").setCellRenderer(dtcr);
JT.getColumn("Offer").setCellRenderer(dtcr);
JT.getColumn("Mk.Vol").setCellRenderer(dtcr);
JT.getColumn("Mk.Val('000)").setCellRenderer(dtcr);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
    }
           
        }
            
        
    String[] Col1 = {"SymbolName","Mark","Open","High","Low","Value","Chg","%Chg","Bids","Offer","Mk.Vol","Mk.Val('000)"};
       String symbolx;int Block[]=new int[12];
public static String[]sym=new String[12];String x ="";
     
     public String[] readFromFile(String textString,String symbol) throws SQLException, FileNotFoundException, IOException {

        
        BufferedReader bufferedReader = null;
        String xc ="";
        String ya;
       for(int i=0;i<sym.length;i++){
           sym[i]=null;
       }
        
        try {
            //Construct the BufferedReader object
            bufferedReader = new BufferedReader(new StringReader(textString));
            String line = null;
          
            while ((line = bufferedReader.readLine()) != null) {
                  int count1 = 0;int b[] = new int[1000];int c[] =new int[1000];int count2 = 0;
                //System.out.println(line.toString());
                            String linenumber = "";
                            String y ="";
                    if(line.contains("C13_FastQuote_Main.jsp?txtSymbol="+symbol+"\"")){
                            for(int i=0;i<5;i++){
                                char xx=line.charAt(i);
                                
                                if(xx>='0'&&xx<='9'){
                                    linenumber+=line.substring(i, i+1);       
                                }        
                            } //System.out.print(linenumber);  //System.out.println(aa);
                       if(line.startsWith(linenumber)){
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count1;
                         b[count1]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                             if(AlertRunnable.isDigit(line.substring(b[i]+1,c[i]))){
                                 ya=line.substring(b[i]+1,c[i]);
                                 if(ya.equals(symbol)){
                                     sym[0]=ya;
                                     
                                 }
                             }
                        } }
                  //  x+="@"+y;
                    

                    for(int k=1;k<=11;k++){
                        Block[k]=Integer.parseInt(linenumber)+k;
                        
                        
                        
                    }
                     }
                    // System.out.println(Block[1]);
                   
                            ////////////////////////////////////
                    // System.out.print(""+y);
                     } 
                        for(int k=1;k<=11;k++){
                       
                        if(line.startsWith(String.valueOf(Block[k])+"-->")){
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count1;
                         b[count1]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                             if(AlertRunnable.isDigit(line.substring(b[i]+1,c[i]))){              
                                      xc=line.substring(b[i]+1,c[i]);
                                      
                             }}
                             }

                        if((!xc.equals("XD")||!xc.equals("SP")||!xc.equals("NP")||!xc.equals("SP,NP,NC")||!xc.equals("XW")
                           ||!xc.equals("SP,NC")||!xc.equals("SP,NP")||!xc.equals("NP,NC" ))&& k==1){


                           y="NOTVALUE";
                          
                       }else{
                           y=xc;
                           
                       }
                        
                    //x+="@"+y;
                  sym[k]=y;
                   if(k==11){
                    break;

                           }
                     //  System.out.print(y);
                     }
                        
                   
                    }
                 
 
        
            
         
            }
        }
 catch (FileNotFoundException ex) {
            Logger.getLogger(AlertRunnable.class.getName()).log(Level.SEVERE, null, ex);
        }         catch (IOException ex) {
        } finally {
            //Close the BufferedReader
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException ex) {
            }
        }
        return sym;
    }
     
     
     
     
}
